package com.icomvn.game.pikachu.play;


public class Algorithms {
	static final int[] UU = {1, -1, 0, 0};
	static final int[] VV = {0, 0,1,-1};
	
//	public void fixMatrix(int[][] a, int fixType) {
//		if (fixType == 1) return;
//		
//		if (fixType == 2) {
//			fixZone(a, 1, 1, 12, 8, 0);
//		} else if (fixType == 3) {
//			fixZone(a, 1, 1, 12, 8, 1);
//		} else if (fixType == 4) {
//			fixZone(a, 1, 1, 12, 8, 3);
//		} else if (fixType == 5) {
//			fixZone(a, 1, 1, 12, 8, 2);
//		} else if (fixType == 6) {
//			fixZone(a, 1, 5, 12, 8, 0);
//			fixZone(a, 1, 1, 12, 4, 1);
//		} else if (fixType == 7) {
//			fixZone(a, 1, 5, 12, 8, 1);
//			fixZone(a, 1, 1, 12, 4, 0);
//		} else if (fixType == 8) {
//			fixZone(a, 1, 1, 6, 8, 3);
//			fixZone(a, 7, 1, 12, 8, 2);
//		} else {
//			fixZone(a, 1, 1, 6, 8, 2);
//			fixZone(a, 7, 1, 12, 8, 3);
//		}
//	}
	public void fixMatrix(int[][] a, int fixType) {
		if (fixType == 1) return;
		
		if (fixType == 2) {
			fixZone(a, 1, 1, 8, 12, 0);
		} else if (fixType == 3) {
			fixZone(a, 1, 1, 8, 12, 1);
		} else if (fixType == 4) {
			fixZone(a, 1, 1, 8, 12, 3);
		} else if (fixType == 5) {
			fixZone(a, 1, 1, 8, 12, 2);
		} else if (fixType == 6) {
			fixZone(a, 5, 1, 8, 12, 0);
			fixZone(a, 1, 1, 4, 12, 1);
		} else if (fixType == 7) {
			fixZone(a, 5, 1, 8, 12, 1);
			fixZone(a, 1, 1, 4, 12, 0);
		} else if (fixType == 8) {
			fixZone(a, 1, 1, 8, 6, 3);
			fixZone(a, 1, 7, 8, 12, 2);
		} else {
			fixZone(a, 1, 1, 8, 6, 2);
			fixZone(a, 1, 7, 8, 12, 3);
		}
	}
	
	public void fixMatrix_1(int[][] a, int fixType) {
		if (fixType == 1) return;
		
		if (fixType == 2) {
			fixZone(a, 1, 2, 8, 11, 0);
		} else if (fixType == 3) {
			fixZone(a, 1, 2, 8, 11, 1);
		} else if (fixType == 4) {
			fixZone(a, 1, 2, 8, 11, 3);
		} else if (fixType == 5) {
			fixZone(a, 1, 2, 8, 11, 2);
		} else if (fixType == 6) {
			fixZone(a, 5, 2, 8, 11, 0);
			fixZone(a, 1, 2, 4, 11, 1);
		} else if (fixType == 7) {
			fixZone(a, 5, 2, 8, 11, 1);
			fixZone(a, 1, 2, 4, 11, 0);
		} else if (fixType == 8) {
			fixZone(a, 1, 2, 8, 6, 3);
			fixZone(a, 1, 7, 8, 11, 2);
		} else {
			fixZone(a, 1, 2, 8, 6, 2);
			fixZone(a, 1, 7, 8, 11, 3);
		}
	}
	
	public void fixMatrix_2(int[][] a, int fixType) {
		if (fixType == 1) return;
		
		if (fixType == 2) {
			fixZone(a, 1, 3, 8, 10, 0);
		} else if (fixType == 3) {
			fixZone(a, 1, 3, 8, 10, 1);
		} else if (fixType == 4) {
			fixZone(a, 1, 3, 8, 10, 3);
		} else if (fixType == 5) {
			fixZone(a, 1, 3, 8, 10, 2);
		} else if (fixType == 6) {
			fixZone(a, 5, 3, 8, 10, 0);
			fixZone(a, 1, 3, 4, 10, 1);
		} else if (fixType == 7) {
			fixZone(a, 5, 3, 8, 10, 1);
			fixZone(a, 1, 3, 4, 10, 0);
		} else if (fixType == 8) {
			fixZone(a, 1, 3, 8, 6, 3);
			fixZone(a, 1, 7, 8, 10, 2);
		} else {
			fixZone(a, 1, 3, 8, 6, 2);
			fixZone(a, 1, 7, 8, 10, 3);
		}
	}
	public static void fixZone(int[][] a, int i1, int j1, int i2, int j2,
			int vector) {
		int i, j, p, q;
		boolean stop;
		do {
			stop = true;
			for (i = i1; i <= i2; i++) {
				for (j = j1; j <= j2; j++) {
					if (a[i][j] > 0) {
						p = i + UU[vector];
						q = j + VV[vector];
						if (p >= 1 && p <= 8 && q >= 1 && q <= 12) {
							if (a[p][q] == 0) {
								swap(a, i, j, p, q);
								stop = false;
							}
						}
					}
				}
			}
		} while (!stop);
	}

	public static void swap(int[][] a, int i1, int j1, int i2, int j2) {
		int tmp = a[i1][j1];
		a[i1][j1] = a[i2][j2];
		a[i2][j2] = tmp;
	}
	
}

